package com.tuananh.radnom.gwt.server;

import java.util.ArrayList;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tuananh.radnom.gwt.client.GreetingService;
import com.tuananh.radnom.gwt.shared.FieldVerifier;
import com.tuananh.radnom.gwt.shared.dto.NetConnDTO;
import com.tuananh.radnom.gwt.shared.dto.NetNodeDTO;
import com.tuananh.radnom.model.NetConn;
import com.tuananh.radnom.model.NetNode;
import com.tuananh.radnom.service.config.INetConnService;
import com.tuananh.radnom.service.config.INetNodeService;
import com.tuananh.radnom.service.config.impl.NetConnService;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {
	INetNodeService netNodeService;
	{
		netNodeService = (INetNodeService) ContextUtils.getContext().getBean(
				"netNodeConfigService");
	}

	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid.
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back
			// to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}

		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");

		// Escape data from the client to avoid cross-site script
		// vulnerabilities.
		input = escapeHtml(input);
		userAgent = escapeHtml(userAgent);

		return "Hello, " + input + "!<br><br>I am running " + serverInfo
				+ ".<br><br>It looks like you are using:<br>" + userAgent;
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html
	 *            the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	public ArrayList<NetNodeDTO> getAllNode() throws IllegalArgumentException {
		// TODO Auto-generated method stub
		System.out.println("Begin remote");
		ArrayList<NetNode> listNode = netNodeService.getAll();
		System.out.println("hohoho");
		ArrayList<NetNodeDTO> result = new ArrayList<NetNodeDTO>();
		for (NetNode node : listNode) {
			NetNodeDTO dto = ModelDtoConverter.netNode2DTO(node);
			if (dto.getName() != null)
				result.add(dto);
		}
		return result;
	}

	public ArrayList<NetConnDTO> getAllConn() {
		// TODO Auto-generated method stub
		System.out.println("Begin remote CONNECTION");
		INetConnService connService = ContextUtils.getNetConnService();
		
		if (connService!=null){
			ArrayList<NetConn> list = connService.getAll();
			System.out.println("FINISH REMOTE CONNECTION");
			ArrayList<NetConnDTO> listDto = new ArrayList<NetConnDTO>();
			for (NetConn netConn:list){
				NetConnDTO dto = ModelDtoConverter.netConn2DTO(netConn);
				if (dto.getName()!=null)
					listDto.add(dto);
			}
			return listDto;
		}
		return null;
	}

}
